﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

namespace L9SubSet
{
	public class ZQConfig
	{
		#region 引入dll
		
		[DllImport("kernel32")]
		private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
		
		[DllImport("kernel32")]
		private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
		
		[DllImport("kernel32")]
		private static extern int GetPrivateProfileSection(string section, byte[] lpReturnedString, int size, string filePath);

		#endregion
		
		private string configFilePath;
		
		public void Attach(string filePath)
		{
			configFilePath = filePath;
		}
		
		public string GetAppPath()
		{
			string path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
			return path.Substring(0, path.LastIndexOf(@"\"));
		}
		
		public bool IsFileExist(string fileName)
		{
			return File.Exists(fileName);
		}
		
		public string ReadValue(string section, string key, string filePath)
		{
			if (IsFileExist(filePath) == false)
			{
				return null;
			}

			StringBuilder sb = new StringBuilder(255);

			// section=配置节，key=键名，filePath=路径
			GetPrivateProfileString(section, key, "", sb, 255, filePath);
			string str = sb.ToString();
			
			// 删除多余字符
			str = DeleteOtherChar(str);
			
			// 删除注释
			if (str.Contains("//") == true)
			{
				str = str.Substring(0, str.LastIndexOf("//"));
			}

			return str;
		}
		
		public int ReadValueInt(string section, string key, string filePath)
		{
			if (IsFileExist(filePath) == false)
			{
				return 0;
			}

			string str = ReadValue(section, key, filePath);
			
			if (str == null)
			{
				return 0;
			}
			
			return Int32.Parse(str);
		}
		
		public bool ReadValueBool(string section, string key, string filePath)
		{
			if (IsFileExist(filePath) == false)
			{
				return false;
			}

			if (ReadValueInt(section, key, filePath) == 1)
			{
				return true;
			}
			
			return false;
		}

		public string ReadValueString(string section, string key, string filePath)
		{
			if (IsFileExist(filePath) == false)
			{
				return null;
			}

			StringBuilder sb = new StringBuilder(255);

			// section=配置节，key=键名，filePath=路径
			GetPrivateProfileString(section, key, "", sb, 255, filePath);
			string str = sb.ToString();

			// 删除注释
			if (str.Contains("//") == true)
			{
				str = str.Substring(0, str.LastIndexOf("//"));
			}

			return str;
		}

		public void WriteValue(string section, string value, string key, string filePath)
		{
			// section=配置节，key=键名，filePath=路径
			WritePrivateProfileString(section, key, value, filePath);
		}
		
		public string GetParameterString(string section, string key)
		{
			if (IsFileExist(configFilePath))
			{
				return ReadValue(section, key, configFilePath);
			}
			
			return null;
		}
		
		public int GetParameterInt(string section, string key)
		{
			if (IsFileExist(configFilePath))
			{
				return ReadValueInt(section, key, configFilePath);
			}
			
			return 0;
		}
		
		public void GetSection(string section, string fileName, List<string> chipKeyList, List<string> chipValueList)
		{
			byte[] chipInfoBytes = new byte[5000];
			GetPrivateProfileSection(section, chipInfoBytes, 5000, fileName);

			int length = 0;
			string tmpStr, value;
			while (chipInfoBytes[length] != 0)
			{
				int k = length;			// 单行的起始位置
				int oneLineLength = 0;	// 单行的字符串长度
				// 取得一行的长度
				while (Convert.ToChar(chipInfoBytes[length]) != '\0')
				{
					length++;
					oneLineLength++;
				}
				char[] tmpChar = new char[oneLineLength];
				for (int i = 0; i < oneLineLength; i++)
				{
					tmpChar[i] = Convert.ToChar(chipInfoBytes[k + i]);
				}
				tmpStr = new string(tmpChar);
				int pos = tmpStr.IndexOf('=');	// 解析等号位置
				// 判断等号右侧是否为空，为空置零
				if (tmpStr.Substring(pos + 1, oneLineLength - pos - 2) != "")
				{
					value = tmpStr.Substring(pos + 1, oneLineLength - pos - 3);// 截取右侧字符串
					int commentPos = value.IndexOf("//");	// 不为空判断是否有注释符号
					if (commentPos != 0 && commentPos != -1)
					{
						value = value.Substring(0, commentPos);
					}
				}
				else
				{
					value = "0";
				}
				chipKeyList.Add(tmpStr.Substring(0, pos));
				chipValueList.Add(value);
				length++;
			}
		}

		private string DeleteOtherChar(string str)
		{
			str = str.Replace("\t", "");
			str = str.Replace("\n", "");
			str = str.Replace("\r", "");
			str = str.Trim();
			return str;
		}
	}
}
